//
//  TTLiveToolsView.swift
//  TTProduct
//
//  Created by Toj on 3/19/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

protocol TTLiveToolsViewDelegate: NSObjectProtocol {
    func toolsViewDidTapIM(view: TTLiveToolsView)
    func toolsViewDidTapLives(view: TTLiveToolsView)
    func toolsViewDidTapSwitch(button: TTButton)
}

class TTLiveToolsView: NSView {
    
    weak var delegate: TTLiveToolsViewDelegate?
    private lazy var imButton: TTButton = {
        let view = setupMakeButton()
        view.tag = 0
        view.tt_setTitle("讨论区", for: .normal)
        view.tt_setImage(NSImage(named: "liveIMButtonNormal"), for: .normal)
        view.tt_setImage(NSImage(named: "liveIMButtonSelected"), for: .selected)
        view.tt_addTarget(self, action: #selector(didTapIM), for: .leftMouseUp)
        return view
    }()
    
    private lazy var listButton: TTButton = {
        let view = setupMakeButton()
        view.tag = 1
        view.tt_setTitle(" 同学", for: .normal)
        view.tt_setImage(NSImage(named: "liveListButtonNormal"), for: .normal)
        view.tt_setImage(NSImage(named: "liveListButtonSelected"), for: .selected)
        view.tt_addTarget(self, action: #selector(didTapList), for: .leftMouseUp)
        return view
    }()
    
    private lazy var lineView: NSView = {
       let view = NSView()
        view.tt_backgroundColor = NSColor.init(hexColor: "#4EBEFA")
        return view
    }()
    
    private lazy var switchButton: TTButton = {
        let view = TTButton()
        view.tt_setImage(NSImage(named: "liveSwitchBriefNormal"), for: .normal)
        view.tt_setImage(NSImage(named: "liveSwitchBriefHover"), for: .hover)
        view.tt_addTarget(self, action: #selector(didTapSwitch), for: .leftMouseUp)
        return view
    }()
    
    private var selectedButton: TTButton?  {
        willSet {
            guard selectedButton != newValue else {
                return
            }
            
            selectedButton?.tt_selected = false
            newValue?.tt_selected = true
            let tag = newValue?.tag ?? 0
            let leftMargin = tag * 120
            lineView.snp.remakeConstraints { (make) in
                make.left.equalTo(leftMargin)
                make.height.equalTo(2)
                make.bottom.equalTo(self)
                make.width.equalTo(120)
            }
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        setupMakeAddSubviews()
        
        setupMakeLayoutSubviews()
        
        setupMakeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Private
extension TTLiveToolsView {
    
}

// MARK: - 点击事件
extension TTLiveToolsView {
    
    // 切换讨论区
    @objc private func didTapIM(sender: TTButton) {
        selectedButton = sender
        delegate?.toolsViewDidTapIM(view: self)
    }
    
    // 切换学生列表
    @objc private func didTapList(sender: TTButton) {
        selectedButton = sender
        delegate?.toolsViewDidTapLives(view: self)
    }
    
    // 切换布局
    @objc private func didTapSwitch(sender: TTButton) {
        
        if !listButton.tt_selected {
            didTapList(sender: listButton)
            return
        }
        
        sender.tt_selected = !sender.tt_selected
        delegate?.toolsViewDidTapSwitch(button: sender)
        if sender.tt_selected {
            sender.tt_setImage(NSImage(named: "liveSwitchDetailedNormal"), for: .normal)
            sender.tt_setImage(NSImage(named: "liveSwitchDetailedHover"), for: .hover)
            return
        }
        sender.tt_setImage(NSImage(named: "liveSwitchBriefNormal"), for: .normal)
        sender.tt_setImage(NSImage(named: "liveSwitchBriefHover"), for: .hover)
    }
}

// MARK: - setup
extension TTLiveToolsView {
    
    private func setupMakeAddSubviews() {
        addSubview(imButton)
        addSubview(listButton)
        addSubview(lineView)
        addSubview(switchButton)
    }
 
    private func setupMakeLayoutSubviews() {
        let margin = 20
        imButton.snp.remakeConstraints { (make) in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(100)
        }
        
        listButton.snp.remakeConstraints { (make) in
            make.top.bottom.width.equalTo(imButton)
            make.left.equalTo(imButton.snp.right).offset(margin)
        }
        
        switchButton.snp.remakeConstraints { (make) in
            make.centerY.right.equalTo(self)
            make.width.equalTo(28)
            make.height.equalTo(28)
        }
    }
    
    private func setupMakeUI() {
        selectedButton = imButton
    }
    
    private func setupMakeButton() -> TTButton {
        let view = TTButton()
        view.tt_spacing = 7
        view.tt_enabledHover = false
        view.tt_titleAlignment = .right
        view.tt_titleFont = NSFont.systemFont(ofSize:12)
        view.tt_setTitleColor(NSColor.init(hexColor: "#9EBAE0"), for: .normal)
        view.tt_setTitleColor(NSColor.init(hexColor: "#4EBEFA"), for: .selected)
        
        return view
    }
}
